package com.ideal.manage.esms.controller.alarm;

import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.alarm.Threshold;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.service.alarm.AlarmThresholdService;
import com.ideal.manage.esms.service.system.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("alarm")
public class ThresholdController {


    @Resource
    private AlarmThresholdService alarmThresholdService;

    @Resource
    private UserService userService;

    /**
     * 阈值列表请求
     *
     * @param model
     */
    @RequestMapping("threshold_manager")
    public void thresholdList(Model model , String name ) {
        //获取项目信息和设备信息,初始化zTree树
        List<JSONObject> aTeManage = alarmThresholdService.getProjectAndEquipment(name);
        model.addAttribute("aTeManage", aTeManage);

    }

    /**
     *  树结构的搜索
     * @param name
     * @return
     */
    @RequestMapping("threshold_manager_list")
    @ResponseBody
    public List<JSONObject> getThresholdList(String name){
        List<JSONObject> list = alarmThresholdService.getProjectAndEquipment(name);
        return list;
    }

    /**
     * 阈值列表请求
     *
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("threshold_data")
    @ResponseBody
    public PageDto thresholdData(int pageNum, HttpServletRequest request) {
        PageDto pageDto = alarmThresholdService.findThresholdAllData(pageNum, request);
        return pageDto;
    }

    /**
     * 阈值修改请求
     *
     * @param id
     * @param model
     */
    @RequestMapping("threshold_edit")
    public void menuEdit(Model model,
                         @RequestParam(value = "id", required = false) String id) {
        Threshold threshold = alarmThresholdService.findThresholdOneData(id);
        model.addAttribute("threshold", threshold);

    }

    @RequestMapping("threshold_put")
    @ResponseBody
    public String putThreshold(String id, String pId) {
        //阈值管理入库
        alarmThresholdService.getThresholdAndEquipment(id, pId);
        return "";
    }

    //模态框保存
    @RequestMapping("threshold_editManage")
    @ResponseBody
    public Result editManage(String id, String setVal) {
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        alarmThresholdService.editSave(id, setVal);
        return result;
    }

    //初始化按钮
    @RequestMapping("threshold_reset")
    @ResponseBody
    public String resetBtn(String id) {
        alarmThresholdService.resetValue(id);
        return "";
    }

    /**
     * 修改口令校验
     * @param changePassword
     * @return
     */
    @RequestMapping("change_password")
    @ResponseBody
    public String changePassword(@RequestParam("changePassword") String changePassword) {
        if (changePassword != null && StringUtils.isNotBlank(changePassword)) {
            List<User> pwds = alarmThresholdService.findByChanPwd(changePassword);
            for (User user:pwds) {
                if (user.getChangePassword().contains(changePassword)) {
                    return "口令正确";
                }
            }

            return "口令错误,请重新输入";
        }
        return null;
    }

}
